// pages/complain/index.js
import {
    platform,
    wxapp
} from '../../api/apiData'
const upLoadUrl = getApp().globalData.upLoadUrl
Page({

    /**
     * 页面的初始数据
     */
    data: {
        district: '',
        show: false,
        columns: [],
        contact_name: '',
        contact_phone: '',
        content: '',
        images: '',
        fileList: [],
        fileList2: [],
        fileListX: [],
        fileList2X: [],
        fileListX2: [],
        fileList2X2: [],
        name: '',
        id_number: '',
        telephone: '',
        car_number: '',
        work_year: '',
        info: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        //   district.all({}, res => {
        //     this.setData({
        //       columns: res.data
        //     })
        //   })
        wxapp.templateId({}, res => {
            if (res.code == 1) {
                this.setData({
                    tmplIds: [res.data.check_result_id]
                })

            } else {

            }
        })
    },
    toReco() {
        let that = this
        wx.requestSubscribeMessage({
            tmplIds: that.data.tmplIds,
            success(res) {
                console.log('授权成功')
                console.log(res)
                if (res.k_2Q36XOo2ii3v9hxL09YsEwb0PX3xCL1SxZfHT8vGk == "accept") {
                    wx.showToast({
                        title: '订阅成功！',
                    })
                } else {
                    wx.showToast({
                        title: '订阅失败',
                        icon: 'error'
                    })
                }

            },
            fail(res) {
                console.log(res)
                wx.showToast({
                    title: '订阅失败',
                    icon: 'error'
                })
            }
        })
    },
    toPre(e) {
        let img = e.currentTarget.dataset.img
        wx.previewImage({
            urls: [img],
        })
    },
    // 数据绑定
    vmodel(e) {
        this.setData({
            [e.currentTarget.dataset.value]: e.detail.value
            /**
             * e.currentTarget.dataset.value 拿到我要改变的数据 input 
             * e.detail.value 拿到 我改变以后的值
             */
        })
    },
    // 上传图片
    afterRead(event) {
        wx.showLoading({
            title: "上传中...",
            mask:true
          })
        let that = this
        const {
            file
        } = event.detail;
        console.log(file)
        file.forEach(item => {
            wx.uploadFile({
                url: upLoadUrl, // 仅为示例，非真实的接口地址
                filePath: item.url,
                name: 'file',
                formData: {
                    user: 'test'
                },
                success(res) {
                    wx.hideLoading()
                    console.log(res)
                    const {
                        fileList = [],
                            fileList2 = []
                    } = that.data;
                    fileList.push({
                        url: JSON.parse(res.data).data.url
                    });
                    fileList2.push(JSON.parse(res.data).data.path)
                    console.log(fileList2)
                    that.setData({
                        fileList,
                        fileList2,
                    });
                },
                fail(res){
                    wx.showToast({
                      title: '上传失败',
                      icon:'error'
                    })
                    wx.hideLoading()
                }
            });
        })
    },
    deleteImg(event) {
        let index = event.detail.index
        let arr = this.data.fileList
        let arr2 = this.data.fileList2
        arr.splice(index, 1)
        arr2.splice(index, 1)
        this.setData({
            fileList: arr,
            fileList2: arr2
        })
    },
    afterReadX(event) {
        wx.showLoading({
            title: "上传中...",
            mask:true
          })
        let that = this
        const {
            file
        } = event.detail;
        console.log(file)
        file.forEach(item => {
            wx.uploadFile({
                url: upLoadUrl, // 仅为示例，非真实的接口地址
                filePath: item.url,
                name: 'file',
                formData: {
                    user: 'test'
                },
                success(res) {
                    console.log(res)
                    wx.hideLoading()
                    const {
                        fileListX = [],
                            fileList2X = []
                    } = that.data;
                    fileListX.push({
                        url: JSON.parse(res.data).data.url
                    });
                    fileList2X.push(JSON.parse(res.data).data.path)
                    console.log(fileList2X)
                    that.setData({
                        fileListX,
                        fileList2X,
                    });
                },
                fail(res){
                    wx.showToast({
                      title: '上传失败',
                      icon:'error'
                    })
                    wx.hideLoading()
                }
            });
        })
    },
    deleteImgX(event) {
        let index = event.detail.index
        let arr = this.data.fileListX
        let arr2 = this.data.fileList2X
        arr.splice(index, 1)
        arr2.splice(index, 1)
        this.setData({
            fileListX: arr,
            fileList2X: arr2
        })
    },
    afterReadX2(event) {
        wx.showLoading({
            title: "上传中...",
            mask:true
          })
        let that = this
        const {
            file
        } = event.detail;
        console.log(file)
        file.forEach(item => {
            wx.uploadFile({
                url: upLoadUrl, // 仅为示例，非真实的接口地址
                filePath: item.url,
                name: 'file',
                formData: {
                    user: 'test'
                },
                success(res) {
                    wx.hideLoading()
                    console.log(res)
                    const {
                        fileListX2 = [],
                            fileList2X2 = []
                    } = that.data;
                    fileListX2.push({
                        url: JSON.parse(res.data).data.url
                    });
                    fileList2X2.push(JSON.parse(res.data).data.path)
                    console.log(fileList2X2)
                    that.setData({
                        fileListX2,
                        fileList2X2,
                    });
                },
                fail(res){
                    wx.showToast({
                      title: '上传失败',
                      icon:'error'
                    })
                    wx.hideLoading()
                }
            });
        })
    },
    deleteImgX2(event) {
        let index = event.detail.index
        let arr = this.data.fileListX2
        let arr2 = this.data.fileList2X2
        arr.splice(index, 1)
        arr2.splice(index, 1)
        this.setData({
            fileListX2: arr,
            fileList2X2: arr2
        })
    },
    toShow() {
        this.setData({
            show: true
        })
    },
    onConfirm(e) {
        let value = e.detail.value
        this.setData({
            district_id: value.id,
            district: value.name,
            show: false
        })
    },
    onCancel() {
        this.setData({
            show: false
        })
    },
    // 提交建议
    toSuggest() {
        let obj = {
            avatar: this.data.fileList2.toString(),
            driving_license_img: this.data.fileList2X.toString(),
            vehicle_license_img: this.data.fileList2X2.toString(),
            name: this.data.name,
            id_number: this.data.id_number,
            telephone: this.data.telephone,
            car_number: this.data.car_number,
            work_year: this.data.work_year,
        }

        console.log(obj)
        if (obj.name == '') {
            wx.showToast({
                title: '请输入姓名',
                icon: 'error'
            })
            return false
        }
        let that = this

        wx.showModal({
            title: '提示',
            content: '确定要提交吗？',
            showCancel: true, //是否显示取消按钮
            cancelText: "否", //默认是“取消”
            confirmText: "是", //默认是“确定”
            success: function (res) {
                if (res.cancel) {
                    //点击取消,默认隐藏弹框

                } else {
                    //点击确定
                    platform.driver.register(obj, res => {
                        if (res.code == 1) {
                            wx.showToast({
                                title: res.msg,
                            })
                            that.toReco()
                            setTimeout(() => {
                                that.getInfo()
                            }, 1000)
                        } else {
                            wx.showToast({
                                title: res.msg,
                                icon: 'error'
                            })
                        }
                    })
                }
            },
            fail: function (res) {}, //接口调用失败的回调函数
            complete: function (res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
        })
    },
    getInfo() {
        platform.driver.info({}, res => {
            if (res.code == 1) {
                if (res.data != null) {
                    this.setData({
                        info: res.data
                    })
                }
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error'
                })
            }
        })
    },
    // 提交投诉
    toComplain() {
        let obj = {
            contact_name: this.data.contact_nameX,
            contact_phone: this.data.contact_phoneX,
            content: this.data.contentX,
            images: this.data.fileList2X.toString(),
            address: this.data.address,
            district_id: this.data.district_id
        }
        if (obj.contact_name == '') {
            wx.showToast({
                title: '请输入联系人',
                icon: 'error'
            })
            return false
        }
        if (obj.contact_phone == '') {
            wx.showToast({
                title: '请输入联系人电话',
                icon: 'error'
            })
            return false
        }
        if (obj.content == '') {
            wx.showToast({
                title: '请输入投诉事由',
                icon: 'error'
            })
            return false
        }
        wx.showModal({
            title: '提示',
            content: '确定要提交投诉吗？',
            showCancel: true, //是否显示取消按钮
            cancelText: "否", //默认是“取消”
            confirmText: "是", //默认是“确定”
            success: function (res) {
                if (res.cancel) {
                    //点击取消,默认隐藏弹框

                } else {
                    //点击确定
                    complaint.submit(obj, res => {
                        if (res.code == 1) {
                            wx.showToast({
                                title: res.msg,
                            })
                            setTimeout(() => {
                                wx.navigateTo({
                                    url: '/pages/mySuggest/index?type=2',
                                })
                            }, 1000)
                        } else {
                            wx.showToast({
                                title: res.msg,
                                icon: 'error'
                            })
                        }
                    })
                }
            },
            fail: function (res) {}, //接口调用失败的回调函数
            complete: function (res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getInfo()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})